﻿/***********************************************************************************************
 * 
 * ProjectName: WMSSoft.Lib.Basic
 * 
 * Description: Static class for useful drawing basic routines 
 * 
 * Created:     04.11.2012
 * 
 * Author:      matthias wagler - matthias@WMSSoft.de
 * 
 * Copyright:   Copyright 2012 by WMSSoft.net
 * 
 * License:     GPLv3
 * 
 * History:     04.11.2012 - matthias    -   Created
 * 
 **********************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WMSSoft.Lib.Basic
{
    public static class basic_GraphicHelpers
    {
        /// <summary>
        /// Get a Color Value from RGB - Triple
        /// </summary>
        /// <param name="value">input RGB - Triple</param>
        /// <returns>WPF - Color value</returns>
        public static System.Windows.Media.Color GetColorFromString ( string value )
        {
            try
            {
                byte r, g, b;
                int sepPos = -1;
                int sepPos2 = -1;
                sepPos = value.IndexOf(",");
                if (sepPos > -1)
                {
                    r = Convert.ToByte(value.Substring(0, sepPos));
                    sepPos2 = value.IndexOf(",", sepPos + 1);
                    if (sepPos2 > -1)
                    {
                        g = Convert.ToByte(value.Substring(sepPos + 1, sepPos2 - (sepPos + 1)));

                        if (sepPos < value.Length)
                        {
                            b = Convert.ToByte(value.Substring(sepPos2 + 1, value.Length - (sepPos2 + 1)));

                            return System.Windows.Media.Color.FromRgb(r, g, b);
                        }
                        else return System.Windows.Media.Color.FromRgb(0, 0, 0);
                    }
                    else return System.Windows.Media.Color.FromRgb(0, 0, 0); 
                }
                else return System.Windows.Media.Color.FromRgb(0, 0, 0);
            }
            catch
            {
                return System.Windows.Media.Color.FromRgb(0, 0, 0);
            }
        }

        /// <summary>
        /// Test a string for a given RGB triple
        /// </summary>
        /// <param name="value">RGB Text value to test</param>
        /// <returns>True if given value is a RGB Triple otherwise false</returns>
        public static bool isColorTriple ( string value )
        {
            try
            {
                if (GetColorFromString(value) == null)
                    return false;
                else
                    return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Converts a Image to a WPFImage
        /// </summary>
        /// <param name="Image"></param>
        /// <returns></returns>
        public static BitmapImage WPFBitmapFromDrawingImage ( System.Drawing.Image Image )
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
                bImg.BeginInit();
                bImg.StreamSource = new MemoryStream(ms.ToArray());
                bImg.EndInit();
                return bImg;
            }
        }

        /// <summary>
        /// Converts an Windows Icon to a WPF Image
        /// </summary>
        /// <param name="gIcon">Windows Icon to convert</param>
        /// <returns>WPF Image</returns>
        public static BitmapImage GetWPFImageFromIcon(Icon gIcon)
        {
            try
            {
                Bitmap myBmp = gIcon.ToBitmap();
                System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                myBmp.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);

                BitmapImage myBmpImage = new BitmapImage();
                myBmpImage.BeginInit();
                memStream.Seek(0, System.IO.SeekOrigin.Begin);
                myBmpImage.StreamSource = memStream;
                myBmpImage.EndInit();

                return myBmpImage;
            }
            catch (Exception ex)
            {
                CMessagelogging.AppendMessage("Can't convert Icon to WPF BitmapImage!'", ex, EMessageTypes.Error);
                return null;
            }
        }
    }
}
